/**
 * 渠道发布，发布记录与获取记录
 */
import React from 'react';
import { Button } from 'antd';
import { history } from 'umi'
import { PageContainer } from '@/molecules';
import { HistoryPublish, HistoryResume } from './containers';

const tabList = [
    {
        key: '1',
        tab: '岗位发布记录'
    },
    {
        key: '2',
        tab: '简历获取记录'
    }
];

const HistoryDetail = ({ location }) => {
    const { query } = location;
    const [activeKey, setActiveKey] = React.useState('1');
    const [savedQuery, setSavedQuery] = React.useState(null);

    const handleTabChange = key => {
        setActiveKey(key);
        const obj = {};

        // 渠道页查看对应招聘需求下的操作记录
        if (query?.recruitCode) {
            obj.recruitCode = query.recruitCode;
            if (query?.channelCompanyCode) {
                obj.channelCompanyCode =
                    query.channelCompanyCode instanceof Array ? query.channelCompanyCode[0] : query.channelCompanyCode;
            }
            history.push({ query: { ...savedQuery?.[key], ...obj, activeKey: key } });
            return;
        }

        history.push({ query: { activeKey: key } });
    };

    const CONTENT = {
        1: <HistoryPublish key="publish" />,
        2: <HistoryResume key="resume" />
    };

    React.useEffect(() => {
        const key = query?.activeKey || '1';
        setActiveKey(key);
        setSavedQuery({
            ...savedQuery,
            [key]: query
        });
    }, [query]);

    return (
        <PageContainer
            tabList={tabList}
            activeTabKey={activeKey}
            onTabChange={handleTabChange}
            tabBarExtraContent={
                query?.recruitCode && (
                    <Button
                        type="primary"
                        style={{ marginTop: 12 }}
                        onClick={() => {
                            history.push({
                                pathname: '/office/personnelManagement/recruitment/channel/history',
                                query: {
                                    channelCompanyCode:
                                        query?.channelCompanyCode instanceof Array
                                            ? query.channelCompanyCode[0]
                                            : query.channelCompanyCode
                                }
                            });
                        }}
                    >
                        全部统计
                    </Button>
                )
            }
        >
            {CONTENT[activeKey]}
        </PageContainer>
    );
};

export default HistoryDetail;
